package com.bank.utils;

import com.bank.entity.Member;
import com.bank.entity.vo.Response;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.util.DigestUtils;

import java.awt.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: Lixy
 * @email: 1908598362@qq.com
 * @date: 2022/1/30 星期日 21:39
 */
public class UserUitls {
    private static void createUser(int count) throws SQLException, ClassNotFoundException, IOException {
        List<Member> members = new ArrayList<>(count);
        for (int i = 0; i < count; i++) {
            Member member = new Member();
            member.setId("1872394235971"+i);
            member.setName("Member" + i);
            member.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
            member.setPhone("18700000000" + i);
            member.setAge(18L);
            members.add(member);
        }

        System.out.println("create Member...");

        Connection conn = getConn();

        String sql = "insert into member(name,password,phone,age,id) VALUES (?, ?, ?, ?,?)";

        PreparedStatement ppstm = conn.prepareStatement(sql);

        for (int i = 0; i < members.size(); i++) {
            Member member = members.get(i);
            ppstm.setString(1, member.getName());
            ppstm.setString(2, member.getPassword());
            ppstm.setString(3, member.getPhone());
            ppstm.setLong(4, member.getAge());
            ppstm.setString(5,member.getId());
            ppstm.addBatch();
        }
        ppstm.executeBatch();
        ppstm.clearParameters();
        conn.close();
        System.out.println("inserting to db...");

        String urlString = "http://localhost:8080/member/uidToLogin";
        File file = new File("C:\\Users\\Lxy\\Desktop\\config.txt");
        if (file.exists()) {
            file.delete();
        }
        RandomAccessFile raf = new RandomAccessFile(file, "rw");
        raf.seek(0);
        for (int i = 0; i < members.size(); i++) {
            Member member = members.get(i);
            URL url = new URL(urlString);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("POST");
            urlConnection.setDoOutput(true);
            OutputStream out = urlConnection.getOutputStream();
            String params = "MemberId" + member.getId() + "&password=" + DigestUtils.md5DigestAsHex("123456".getBytes());
            out.write(params.getBytes());
            out.flush();
            InputStream inputStream = urlConnection.getInputStream();
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte[] buff = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buff)) >= 0) {
                bout.write(buff, 0, len);
            }
            inputStream.close();
            bout.close();
            String responese = new String(bout.toByteArray());
            ObjectMapper mapper = new ObjectMapper();
            Response response1 = mapper.readValue(responese, Response.class);
            String uuid = String.valueOf(response1.getData());
            System.out.println("create uuid" + uuid);
            String row = member.getId() + "," + uuid;
            raf.seek(raf.length());
            raf.write(row.getBytes(StandardCharsets.UTF_8));
            raf.write("\f\n".getBytes());
            System.out.println("write to file:" + member.getId());
        }

        raf.close();
        System.out.println("结束");
    }

    private static Connection getConn() throws ClassNotFoundException, SQLException {
        String url = "jdbc:mysql://localhost:3306/bankserv?serverTimezone=Asia/Shanghai";
        String username = "root";
        String password = "123456";
        String driver = "com.mysql.cj.jdbc.Driver";
        Class.forName(driver);
        return DriverManager.getConnection(url, username, password);
    }


    public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {
        createUser(100);
    }
}
